<?php

/**
 * 这是一个表 "party" 的模型类。
 *
 * 表 'party' 有以下字段:
 * @property integer $id
 * @property integer $ad_id
 * @property integer $line_id
 * @property string $user_code
 * @property integer $crowd_id
 * @property string $username
 * @property string $phone
 * @property string $vcode
 * @property string $code
 * @property string $head_image
 * @property string $sex
 * @property string $province
 * @property string $city
 * @property string $area
 * @property string $address
 * @property string $company
 * @property integer $children_num
 * @property string $recommend
 * @property integer $is_float
 * @property integer $is_send
 * @property integer $is_from
 * @property integer $is_use
 * @property integer $is_origin
 * @property string $add_time
 * @property string $update_id
 * @property string $update_time
 * @property integer $age
 *
 * 以下是表的关联关系:
 * @property Children[] $childrens
 * @property LineDetail $line
 * @property ActivityDetail $ad
 * @property Crowd $crowd
 * @property Sale[] $sales
 */
class Party extends CActiveRecord
{
	/**
	 * @return string 表名
	 */
	public function tableName()
	{
		return 'party';
	}

	/**
	 * @return array 表字段验证规则的数组。
	 */
	public function rules()
	{
		return array(
                        array('username,line_id,phone, company, code, province, city, area, address,vcode,ad_id,', 'required'),
			array('ad_id, crowd_id, children_num, is_float, is_send, is_from, is_use, is_origin', 'numerical', 'integerOnly'=>true),
			array('user_code, head_image, address, company', 'length', 'max'=>255),
			array('username', 'length', 'max'=>5),
			array('phone', 'length', 'max'=>11),
                        array('phone,recommend', 'match', 'pattern'=>'/^1[0-9]{10}$/','message'=>'请输入有效的{attribute}'),
			array('vcode', 'length', 'max'=>10),
			array('update_id', 'length', 'max'=>50),
                        array('code', 'length', 'min'=>18,'max'=>18),
			array('sex', 'length', 'max'=>2),
			array('province, city, area, recommend', 'length', 'max'=>20),
			array('add_time, update_time', 'safe'),
                        array('ad_id', 'checkPhone', 'on'=>'register'),
                        array('vcode', 'checktagname', 'on'=>'register'),
			// 以下规则用于验证查询函数，如果不做查询可注释此行。
			array('id, ad_id,line_id,user_code, crowd_id, username, phone, vcode, code, head_image, sex, province, city, area, address, company, children_num, recommend, is_float, is_send, is_from, is_use, is_origin, add_time, update_id, update_time', 'safe', 'on'=>'search'),
		);
	}
         public function checkPhone($attribute,$params){
               if(!empty($this->phone)&& !empty($this->ad_id)){
                $count=Party::model()->count("phone='".$this->phone."' and ad_id=".$this->ad_id);
                if($count>0){
                   $this->addError($attribute, '该手机号已经报名了本次活动');
                }
               }
        }
        public function checktagname($attribute,$params){
            
            if(!empty($this->phone)&& !empty($this->vcode)){
            $count= Captcha::model()->count('phone="'.$this->phone.'" and code="'.$this->vcode.'"');
            if($count>0){} 
             else
               $this->addError($attribute, '手机验证码错误.');
            }
        }
       

	/**
	 * @return array 关联关系。
	 */
	public function relations()
	{
		return array(
			'childrens' => array(self::HAS_MANY, 'Children', 'party_id'),
                        'line' => array(self::BELONGS_TO, 'LineDetail', 'line_id'),
                        'ad' => array(self::BELONGS_TO, 'ActivityDetail', 'ad_id'),
                        'crowd' => array(self::BELONGS_TO, 'Crowd', 'crowd_id'),
                        'sales' => array(self::HAS_MANY, 'Sale', 'party_id'),
		);
	}

	/**
	 * @return array 自定义字段显示文本标签 (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '序号',
			'ad_id' => '活动',
                        'line_id' => '上车地点',
			'user_code' => '识别码',
			'crowd_id' => '人群属性',
			'username' => '姓名',
			'phone' => '手机号',
			'vcode' => '验证码',
			'code' => '身份证',
			'head_image' => '头像',
                        'age' => '年龄',
			'sex' => '性别',
			'province' => '省份',
			'city' => '城市',
			'area' => '地区',
			'address' => '详细地址',
			'company' => '单位',
			'children_num' => '儿童数量',
			'recommend' => '推荐人',
			'is_float' => '状态',
			'is_send' => '发送状态',
			'is_from' => '是否来访',
			'is_use' => '默认地址',
			'is_origin' => '来源',
			'add_time' => '创建时间',
			'update_id' => '编辑用户',
			'update_time' => '编辑时间',
		);
	}

	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ad_id',$this->ad_id);
                $criteria->compare('line_id',$this->line_id);
		$criteria->compare('user_code',$this->user_code,true);
		$criteria->compare('crowd_id',$this->crowd_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('vcode',$this->vcode,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('head_image',$this->head_image,true);
                $criteria->compare('age',$this->age);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('children_num',$this->children_num);
		$criteria->compare('recommend',$this->recommend,true);
		$criteria->compare('is_float',$this->is_float);
		$criteria->compare('is_send',$this->is_send);
		$criteria->compare('is_from',$this->is_from);
		$criteria->compare('is_use',$this->is_use);
		$criteria->compare('is_origin',$this->is_origin);
		$criteria->compare('add_time',$this->add_time,true);
		$criteria->compare('update_id',$this->update_id,true);
		$criteria->compare('update_time',$this->update_time,true);
                $criteria->addCondition('id in (SELECT id FROM party where 1=1 GROUP BY phone,code ORDER BY  id desc)');
		$criteria->order='id desc';//排序字段
//                $criteria->group='phone,code';
                
            

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
        
        /**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_date()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ad_id',$this->ad_id);
                $criteria->compare('line_id',$this->line_id);
		$criteria->compare('user_code',$this->user_code,true);
		$criteria->compare('crowd_id',$this->crowd_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('vcode',$this->vcode,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('head_image',$this->head_image,true);
                $criteria->compare('age',$this->age);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('children_num',$this->children_num);
		$criteria->compare('recommend',$this->recommend,true);
		$criteria->compare('is_float',$this->is_float);
		$criteria->compare('is_send',$this->is_send);
		$criteria->compare('is_from',$this->is_from);
		$criteria->compare('is_use',$this->is_use);
		$criteria->compare('is_origin',$this->is_origin);
		$criteria->compare('add_time',$this->add_time,true);
		$criteria->compare('update_id',$this->update_id,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->order='id desc';//排序字段

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
        
        
         /**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_print()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ad_id',$this->ad_id);
                $criteria->compare('line_id',$this->line_id);
		$criteria->compare('user_code',$this->user_code,true);
		$criteria->compare('crowd_id',$this->crowd_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('vcode',$this->vcode,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('head_image',$this->head_image,true);
                $criteria->compare('age',$this->age);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('children_num',$this->children_num);
		$criteria->compare('recommend',$this->recommend,true);
		$criteria->compare('is_float',$this->is_float);
		$criteria->compare('is_send',$this->is_send);
		$criteria->compare('is_from',$this->is_from);
		$criteria->compare('is_use',$this->is_use);
		$criteria->compare('is_origin',$this->is_origin);
		$criteria->compare('add_time',$this->add_time,true);
		$criteria->compare('update_id',$this->update_id,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->order='id desc';//排序字段

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                        'pageSize'=>9999999,//配置文件中设置过分页条数
            ),
		));
	}
        
        
        
	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_msg($phone,$code)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ad_id',$this->ad_id);
                $criteria->compare('line_id',$this->line_id);
		$criteria->compare('user_code',$this->user_code,true);
		$criteria->compare('crowd_id',$this->crowd_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('phone',$phone);
		$criteria->compare('vcode',$this->vcode,true);
		$criteria->compare('code',$code);
		$criteria->compare('head_image',$this->head_image,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('children_num',$this->children_num);
		$criteria->compare('recommend',$this->recommend,true);
		$criteria->compare('is_float',$this->is_float);
		$criteria->compare('is_send',$this->is_send);
		$criteria->compare('is_from',$this->is_from);
		$criteria->compare('is_use',$this->is_use);
		$criteria->compare('is_origin',$this->is_origin);
		$criteria->compare('add_time',$this->add_time,true);
		$criteria->compare('update_id',$this->update_id,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->order='add_time desc';//排序字段

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_recommended($recommend)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ad_id',$this->ad_id);
                $criteria->compare('line_id',$this->line_id);
		$criteria->compare('user_code',$this->user_code,true);
		$criteria->compare('crowd_id',$this->crowd_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('vcode',$this->vcode,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('head_image',$this->head_image,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('children_num',$this->children_num);
		$criteria->compare('recommend',$recommend);
		$criteria->compare('is_float',$this->is_float);
		$criteria->compare('is_send',$this->is_send);
		$criteria->compare('is_from',$this->is_from);
		$criteria->compare('is_use',$this->is_use);
		$criteria->compare('is_origin',$this->is_origin);
		$criteria->compare('add_time',$this->add_time,true);
		$criteria->compare('update_id',$this->update_id,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->order='add_time desc';//排序字段

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	} 
	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_age($age_1,$age_2,$recommend)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ad_id',$this->ad_id);
                $criteria->compare('line_id',$this->line_id);
		$criteria->compare('user_code',$this->user_code,true);
		$criteria->compare('crowd_id',$this->crowd_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('vcode',$this->vcode,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('head_image',$this->head_image,true);
                $criteria->compare('age',$this->age);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('children_num',$this->children_num);
		$criteria->compare('recommend',$this->recommend,true);
		$criteria->compare('is_float',$this->is_float);
		$criteria->compare('is_send',$this->is_send);
		$criteria->compare('is_from',$this->is_from);
		$criteria->compare('is_use',$this->is_use);
		$criteria->compare('is_origin',$this->is_origin);
		$criteria->compare('add_time',$this->add_time,true);
		$criteria->compare('update_id',$this->update_id,true);
		$criteria->compare('update_time',$this->update_time,true);
                $criteria->addCondition('age>='.$age_1.' and age<='.$age_2);
                switch ($recommend) {
                    case 1:
                         $criteria->addCondition("recommend !=''");
                        break;
                    case 2:
                         $criteria->addCondition("recommend =''");
                        break;
                }
		$criteria->order='id desc';//排序字段
                $criteria->group='phone,code';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}
	/**
	 * 自定义查询函数
	 * @return CActiveDataProvider 一个包含分页排序查询数据
	 */
	public function search_recommendeds($recommend,$is_children)
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ad_id',$this->ad_id);
                $criteria->compare('line_id',$this->line_id);
		$criteria->compare('user_code',$this->user_code,true);
		$criteria->compare('crowd_id',$this->crowd_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('vcode',$this->vcode,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('head_image',$this->head_image,true);
                $criteria->compare('age',$this->age);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('area',$this->area,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('children_num',$this->children_num);
		$criteria->compare('recommend',$this->recommend,true);
		$criteria->compare('is_float',$this->is_float);
		$criteria->compare('is_send',$this->is_send);
		$criteria->compare('is_from',$this->is_from);
		$criteria->compare('is_use',$this->is_use);
		$criteria->compare('is_origin',$this->is_origin);
		$criteria->compare('add_time',$this->add_time,true);
		$criteria->compare('update_id',$this->update_id,true);
		$criteria->compare('update_time',$this->update_time,true);
                switch ($recommend) {
                    case 1:
                         $criteria->addCondition("recommend !=''");
                        break;
                    case 2:
                         $criteria->addCondition("recommend =''");
                        break;
                }
                switch ($is_children) {
                    case 1:
                        $criteria->addCondition("0<(SELECT COUNT(*) FROM children c where t.id=c.party_id)");
                        break;
                    case 2:
                        $criteria->addCondition("0=(SELECT COUNT(*) FROM children c where t.id=c.party_id)");
                        break;
                }
		$criteria->order='id desc';//排序字段
                $criteria->group='phone,code';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
                'pageSize'=>Yii::app()->params['pagesize'],//配置文件中设置过分页条数
            ),
		));
	}        
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Party the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
